<template>
  <div ref="appRef" class="my-container-box">
    <page-header>
      <div v-permission="{ action: 'add' }" class="btn" @click="handleAdd()">
        <i class="iconfont icon-xinzeng1"></i>新增
      </div>
    </page-header>
    <search-top-form
      class="search-form"
      :search-form="searchObj.searchForm"
      :search-item="searchItem"
      @handle-search="handleSearch"
      @handle-reset="handleReset"
      @re-count-table-height="getTableHeight"
    ></search-top-form>
    <table-page-content
      :loading="tableObj.loading"
      :table-data="tableObj.tableList"
      :table-headers="tableHeaders"
      :table-height="tableHeight"
      :page-config="searchObj.pageConfig"
      @page-size-change="pageSizeChange"
      @current-page-change="currentPageChange"
    >
      <template #operate="{ row }">
        <div class="table-btn-box">
          <el-button v-permission="{ action: 'edit' }" link @click="handleUser(row.roleId)"
            ><span class="text">人员管理</span></el-button
          >
          <el-button v-permission="{ action: 'find' }" link @click="handleDetail(row.roleId)"
            ><span class="text">角色详情</span></el-button
          >
          <el-button v-permission="{ action: 'edit' }" link @click="handleAdd(row.roleId)"
            ><span class="text">编辑</span></el-button
          >
          <el-button v-permission="{ action: 'remove' }" link @click="handleDel(row.roleId)"
            ><span class="text">删除</span></el-button
          >
        </div>
      </template>
    </table-page-content>
    <add-component ref="addRef" @handle-added="getList"></add-component>
    <detail-component ref="detailRef"></detail-component>
    <choose-component ref="chooseRef" @handle-added="getList"></choose-component>
  </div>
</template>

<script lang="ts" setup name="Role">
import { useProxy } from '@/utils/proxy'
import { useTableHeight } from '@/hooks/tableHeight'
import { pageConfig } from '@/utils/config'
import { tableHeaders, searchItem } from './config'
import { getRoleList, deleteRole } from './api'
import addComponent from './components/add.vue'
import detailComponent from './components/detail.vue'
import chooseComponent from './components/choose.vue'
const { appRef, getTableHeight, tableHeight } = useTableHeight()
const { proxy } = useProxy()
const addRef = ref()
const detailRef = ref()
const chooseRef = ref()
// 搜索对象
const searchObj = reactive({
  searchForm: { roleName: null },
  pageConfig: { ...pageConfig }
})
const tableObj = reactive({
  loading: false,
  tableList: []
})
// 搜索
const handleSearch = () => {
  searchObj.pageConfig.pageIndex = 1
  getList()
}
const handleReset = () => {
  searchObj.searchForm = { roleName: null }
  handleSearch()
}
// 获取列表数据
const getList = () => {
  tableObj.loading = true
  tableObj.tableList = []
  getRoleList<any>({
    ...searchObj.searchForm,
    ...searchObj.pageConfig
  })
    .then((res) => {
      tableObj.tableList = res.data
      searchObj.pageConfig.total = res.total
      tableObj.loading = false
    })
    .catch(() => {
      tableObj.loading = false
    })
}
// 表格切换page-size
const pageSizeChange = (val: number) => {
  searchObj.pageConfig.pageSize = val
  searchObj.pageConfig.pageIndex = 1
  getList()
}
// 切换分页
const currentPageChange = (val: number) => {
  searchObj.pageConfig.pageIndex = val
  getList()
}
// 人员管理
const handleUser = (id: string) => {
  chooseRef.value.state.currRoleId = id
  chooseRef.value.getRoleDetail()
  chooseRef.value.state.show = true
}
// 新增/编辑
const handleAdd = (id?: string) => {
  if (id) {
    addRef.value.state.currRoleId = id
    addRef.value.getRoleDetail()
  }
  addRef.value.state.show = true
}
// 删除
const handleDel = (id: string) => {
  proxy.$message.confirm('角色删除后，将无法恢复，请确认是否删除该角色?').then(async () => {
    await deleteRole(id)
    proxy.$message.msgSuccess('删除成功')
    getList()
  })
}
// 查看详情
const handleDetail = (id: string) => {
  detailRef.value.state.currRoleId = id
  detailRef.value.getRoleDetail()
  detailRef.value.state.show = true
}
// 初始化
const init = () => {
  getList()
}
init()
</script>

<style lang="scss" scoped></style>
